package com.woniu.adapter;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.woniu.outlet.dao.PurchaseDetailPoMapper;
import com.woniu.outlet.po.*;
import com.woniu.service.PurchasePoService;
import com.woniu.uitl.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 采购详情适配层
 */
@Component
public class PurchaseDetailAdapter {

    @Autowired
    private PurchaseDetailPoMapper purchaseDetailPoMapper;
    @Autowired
    private PurchaseAdapter purchaseAdapter;
    @Autowired
    private PurchasePoService purchasePoService;

    /**
     * 添加采购详情单
     */
    public void addPurDetail(PurchaseDetailPo detailPo) throws Exception{
            detailPo.setStatus("N");
            //添加采购详情
            purchaseDetailPoMapper.insertSelective(detailPo);
            //修改采购单金额
            purchaseAdapter.countMoney(detailPo.getPurchaseId());
    }

    /**
     * 修改采购详情
     */
    public void updatePurDetail(PurchaseDetailPo detailPo) throws Exception{
        PurchaseDetailPo detailPo1 = purchaseDetailPoMapper.selectByPrimaryKey(detailPo.getId());
        if (!StringUtils.isEmpty(detailPo.getPurchaseNum())){
            BigDecimal money = new BigDecimal(detailPo.getPurchaseNum()).multiply(detailPo1.getPurchaseTaxPrice());
            detailPo.setTotalMoney(money);
        }
        //修改采购详情
        purchaseDetailPoMapper.updateByPrimaryKeySelective(detailPo);
        //修改采购单金额
        purchaseAdapter.countMoney(detailPo1.getPurchaseId());
    }

    /**
     * 删除采购详情
     */
    public void deletePurDetailById(Long id) throws Exception{
        PurchaseDetailPo detailPo1 = purchaseDetailPoMapper.selectByPrimaryKey(id);
        //删除采购详情
        purchaseDetailPoMapper.deleteByPrimaryKey(id);
        //修改采购单金额
        purchaseAdapter.countMoney(detailPo1.getPurchaseId());
    }


}
